package com.plum.admin.modules.sys.controller;


import cn.hutool.core.bean.BeanUtil;
import com.plum.admin.common.annotation.Log;
import com.plum.admin.common.enums.LogType;
import com.plum.admin.common.util.R;
import com.plum.admin.common.util.SecurityUtil;
import com.plum.admin.config.security.permission.MySecurityMetadataSource;
import com.plum.admin.modules.sys.entity.SysMenu;
import com.plum.admin.modules.sys.service.ISysMenuService;
import com.plum.admin.modules.sys.vo.MenuTreeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import com.plum.admin.base.BaseController;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜单权限表 前端控制器
 * </p>
 *
 * @author 子丶沫
 * @since 2021-08-13
 */
@RestController
@RequestMapping("/sys/menu")
@Slf4j
@Api(tags = "菜单管理")
public class SysMenuController extends BaseController {
    @Autowired
    private ISysMenuService menuService;
    @Autowired
    private MySecurityMetadataSource mySecurityMetadataSource;
    @Autowired
    private SecurityUtil securityUtil;
    @GetMapping("queryMenuList")
    @ApiOperation("查询菜单列表")
    @PreAuthorize("hasAnyAuthority('sys:menu:view')")
    @Log(type = LogType.OPERATION,title = "查询菜单列表")
    public R<Object> queryMenuList(){
        return R.ok(menuService.queryMenuList(0));
    }

    @GetMapping("querySelectMenu")
    @ApiOperation("查询选择菜单列表")
    @PreAuthorize("hasAnyAuthority('sys:menu:view')")
    @Log(type = LogType.OPERATION,title = "查询选择菜单列表")
    public R<Object> querySelectMenu(){
        return R.ok(menuService.querySelectMenu(1));
    }

    @GetMapping("queryRouter")
    @ApiOperation("查询菜单列表")
    @Log(type = LogType.OPERATION,title = "查询菜单列表")
    @PreAuthorize("hasAnyAuthority('sys:menu:list')")
    public R<Object> queryRouter(){
        return R.ok(menuService.queryRouter(securityUtil.getUser().getId()));
    }


    @PutMapping
    @ApiOperation("修改菜单")
    @PreAuthorize("hasAnyAuthority('sys:menu:edit')")
    @Log(type = LogType.OPERATION,title = "修改菜单")
    public R<Object> updateMenu(@RequestBody MenuTreeVo menuTreeVo){
        SysMenu menu=new SysMenu();
        BeanUtil.copyProperties(menuTreeVo,menu);
        menu.setId(menuTreeVo.getKey());
        mySecurityMetadataSource.loadResourceDefine();
        return R.ok(menuService.updateById(menu));
    }

    @PostMapping
    @ApiOperation("保存菜单")
    @PreAuthorize("hasAnyAuthority('sys:menu:save')")
    @Log(type = LogType.OPERATION,title = "保存菜单")
    public R<Object> insertMenu(@RequestBody MenuTreeVo menuTreeVo){
        SysMenu menu=new SysMenu();
        BeanUtil.copyProperties(menuTreeVo,menu);
        menu.setId(menuTreeVo.getKey());
        mySecurityMetadataSource.loadResourceDefine();
        return R.ok(menuService.save(menu));
    }
    @DeleteMapping("/{ids}")
    @ApiOperation("删除菜单")
    @Log(type = LogType.OPERATION,title = "删除菜单")
    @PreAuthorize("hasAnyAuthority('sys:menu:delete')")
    public R<Object> deleteMenu(@PathVariable("ids") String ids){
        List<String> idList = Arrays.stream(ids.split(",")).collect(Collectors.toList());
        menuService.batchDelete(idList);
        mySecurityMetadataSource.loadResourceDefine();
        return R.ok(true);
    }
}
